﻿namespace Socut
{
    using System;
    using System.Data;
    using System.Data.SqlClient;

    internal class CSql
    {
        public SqlConnection ConnSql;

        public void Close()
        {
            this.ConnSql.Close();
            this.ConnSql.Dispose();
            this.ConnSql = null;
        }

        public DataSet ExecuteDataSet(string StoreName, int startindex, int num, string dataname, string[,] para, int ConnIndex)
        {
            this.Open(ConnIndex);
            DataSet dataSet = new DataSet();
            SqlCommand cmd = new SqlCommand();
            cmd.CommandTimeout = 0;
            CParameter.SqlserverParameters(StoreName, para, cmd, this.ConnSql);
            new SqlDataAdapter(cmd).Fill(dataSet, startindex, num, dataname);
            this.Close();
            return dataSet;
        }

        public int ExecuteNonQuery(string sql, string[,] para, int ConnIndex)
        {
            this.Open(ConnIndex);
            SqlCommand cmd = new SqlCommand();
            cmd.CommandTimeout = 0;
            CParameter.SqlserverParameters(sql, para, cmd, this.ConnSql);
            int num = cmd.ExecuteNonQuery();
            this.Close();
            return num;
        }

        public object ExecuteScalar(string SQL, string[,] para, int ConnIndex)
        {
            this.Open(ConnIndex);
            SqlCommand cmd = new SqlCommand();
            cmd.CommandTimeout = 0;
            CParameter.SqlserverParameters(SQL, para, cmd, this.ConnSql);
            object obj2 = cmd.ExecuteScalar();
            this.Close();
            return obj2;
        }

        public void Open(int ConnIndex)
        {
            string source = CLink.Source;
            if (source.IndexOf("|") > -1)
            {
                source = source.Split(new char[] { '|' })[ConnIndex];
            }
            this.ConnSql = new SqlConnection(source);
            if (this.ConnSql.State == ConnectionState.Closed)
            {
                this.ConnSql.Open();
            }
        }
    }
}

